import Vue from 'vue'
export default {
	name: "baoMing",
	props: [
		'courseIdd',
		'goodData'
	],

	data() {
		return {
			index: -1,
			title: "填写报名信息",
			gender: ['男', '女'],
			waitTime: 60, //验证码更新的时间
			yanzheng: false, //验证授权状态
			payStatus: false, //支付状态
			msgData: {
				action: 'signCourse',
				openId: this.Reques.sessionData.data.openId,
				courseId: this.courseIdd,
				name: "", //姓名
				gender: "", //性别
				major: "", //专业
				grade: "", //年级
				inviteCode: (this.$userCode == this.Reques.sessionData.data.inviteCode) ? "" : this.$userCode, //邀请码
				phone: "", //手机号
				code: "", //
				subTypeId: this.goodData.subType[0].subTypeId, //课程类型id
				nickname: "", //昵称
				typeIndex: 0, //类型选择的id
				headImgUrl: "", //头像地址
				discountId: "", //优惠券的id
				coupon: {
					title: "请选择你的优惠券",
					price: "",
					id: ""
				}, //优惠券
			},
		}

	},
	mounted() {
		this.msgData.inviteCode = (this.msgData.msgData == this.msgData.openId) ? '' : this.msgData.inviteCode;
		console.log(this.goodData.subType);
		uni.getStorage({
			key: "userInfo",
			success: (res) => {
				console.log(res);
				this.msgData.nickname = res.data.nickName;
				this.msgData.headImgUrl = res.data.avatarUrl;
			}
		});

		//自动加载上一次保存的信息
		uni.getStorage({
			key: 'msgData',
			success: (res) => {
				let arr = ['name', 'gender', 'major', 'grade', 'phone'];
				arr.forEach((e) => {
					this.index = res.data.index;
					this.msgData[e] = res.data.msgData[e];
				});

			}

		})
		console.log("课程价格 ： " + this.goodData);
	},
	methods: {
		/**
		 * 改变报名信息 优惠券的信息
		 */
		changeCoupon(dat) {

			if (dat == undefined) {
				this.msgData.coupon.title = "请选择你的优惠券";
				this.msgData.coupon.price = "";
				this.msgData.coupon.id = "";
				this.msgData.discountId = "";
				return false;
			}

			this.msgData.coupon.title = dat.title;
			this.msgData.coupon.price = dat.price;
			this.msgData.coupon.id = dat.id;
			this.msgData.discountId = dat.id;


		},



		/**
		 * 选择课程类型
		 * @param {Object} index
		 */
		selectType(index) {
			this.msgData.typeIndex = index;
			this.msgData.subTypeId = this.goodData.subType[index].subTypeId;

		},
		/**
		 * 选择优惠券
		 */
		selectConpon() {
			console.log("打开优惠券");
			this.$emit("selectConpon1", this.msgData.coupon.id);

		},


		/**性别的选择
		 * @param {Object} e
		 */
		genderChange(e) {
			this.index = e.detail.value;
			this.msgData.gender = parseInt(this.index) + 1;
		},
		/**获取手机号授权
		 * @param {Object} e
		 */
		getPhoneNumber(e) { //获取手机号授权
			console.log("获取手机号的授权");
			console.log(e.detail.errMsg);
			console.log(e.detail.iv);
			console.log(e.detail.encryptedData);

		},
		clickClose() { //点击关闭
			this.$emit('closeBaoMing');
			
		},
		/**
		 * 提交报名信息
		 * 点击报名
		 */
		sumbit() {
			let showMsg = (msg) => {
				uni.showModal({
					title: "提 示",
					content: msg,
					showCancel: false
				});
			}
			let msg = ""; //提示信息
			for (let i in this.msgData) { //对表单进行验证
				console.log(i + ":" + this.msgData[i]);
				if (this.msgData[i].length == 0) {
					switch (i) {
						case "name":
							msg = "姓名还未填写";
							showMsg(msg);
							return false;
							break;
						case "gender":
							msg = "性别还未选择";
							showMsg(msg);
							return false;
							break;
						case "major":
							msg = "专业还没有填写";
							showMsg(msg);
							return false;
							break;
						case "grade":
							msg = "年级还未填写";
							showMsg(msg);
							return false;
							break;

					}


				}
			}
			if (this.Reques.sessionData.data.inviteCode == this.msgData.inviteCode) {
				msg = "不能使用自己的邀请码报名课程";
				showMsg(msg);
				return false;
			}
			let rule = /^1[3456789]\d{9}$/;
			if (!rule.test(this.msgData.phone)) {
				msg = "手机号格式不正确";
				showMsg(msg);
				return false;
			}

			let saveStorage = () => {
					let dat = {
						msgData: this.msgData,
						index: this.index

					}

					uni.setStorage({
						key: 'msgData',
						data: dat,
						success: () => {
							console.log("保存成功");
						}

					})




				}
			(this.msgData.name != "") ? (saveStorage()) : "";


			this.createOrder();
			//提交数据


		},
		/**
		 * 获取手机验证码
		 */
		getCode() {
			console.log("获取手机验证码");
			let getTime = setInterval(() => {
				if (this.waitTime > 0) {
					this.waitTime--;
				} else {
					clearInterval(getTime);
					this.waitTime = 60;
				}
			}, 1000);
		},
		/**
		 * 创建订单
		 */
		createOrder() {
			Vue.prototype.$pagmentData = {
				goodData: this.goodData,
				userData: this.msgData
			};
			uni.navigateTo({
				url: '/pages/baoMing/payment',
				animationType: 'slide-in-left'
			})


		}
	}




}
